<?php
/**
 * Geneone: Content Management Framework
 * Template Functions
 *
 * http://geneone.neondragon.net/
 *
 * @package Geneone
 * @author Khlo
 * @version 1.0
 * @copyright 2004-2006 Geneone Development Team
 */
 
/**
 * Makes string safe for output and displays.
 *
 * @var string $value Value
 */
function _e($value) {
	echo htmlspecialchars($value);
}

/**
 * Adds arguments to a URL
 *
 * @var string $url URL
 * @var array $params URL Parameters
 */
function addArguments($url, $params=array()) {
	$url = parse_url($url);
	parse_str($url['query'], $query); // Stupid PHP inconsistancy

	$querystring = array();
	$params = array_merge($query, $params);
	foreach ($params as $j => $k) {
		$querystring[] = $j.'='.urlencode($k);
	}
	
	$querystring = implode("&amp;", $querystring);
	
	// Re-"Glue" it back
	$uri = $url['scheme'] ? $url['scheme'].'://' : '';
	$uri .= $url['user'] ? $url['user'].($url['pass']? ':'.$url['pass']:'').'@':'';
	$uri .= $url['host'] ? $url['host'] : '';
	$uri .= $url['port'] ? ':'.$url['port'] : '';
	$uri .= $url['path'] ? $url['path'] : '';
	$uri .= $querystring ? "?".$querystring : '';
	$uri .= $url['fragment'] ? '#'.$url['fragment'] : ''; 
	
	return $uri;
}

/**
 * Checks whether the user can perform a certain action.
 *
 * @var array $object Object Info Array
 * @var string $name Permission Name
 *
 * @return boolean
 */
function checkPermission(&$object, $name) {
	return Gene_Object::checkPermission($object, $name);
}

/**
 * Checks if the user has a certain permission
 *
 * @var string $name Permission Name
 *
 * @return boolean
 */
function hasPermission($name) {
	$user =& Gene::getUser();
	return $user->hasPermission($name);
}

/**
 * Changes a date into a readable format.
 *
 * @var integer $time Unix Timestamp
 * @var string $format PHP Date Format
 *
 * @return string Readable Date
 */
function makeDate($time, $format="F jS Y, H:i") {
	if ($time == "now") {
		$time = time();
	}
	
	return date($format, $time);
}

/**
 * Changes a date into a readable "time ago" format.
 *
 * @var integer $time Unix Timestamp
 *
 * @return string Readable "time ago"
 */
function timeAgo($time) {
	$timeago = time() - $time;
	if ($timeago < 60) {
		$n = floor($timeago);
		$tago = $n.' second'.($n != 1 ? 's' : '');
	} else if ($timeago < 3600) {
		$n = floor($timeago / 60);
		$tago = $n.' minute'.($n != 1 ? 's' : '');
	} else if ($timeago < 86400) {
		$n = floor($timeago / 3600);
		$tago = $n.' hour'.($n != 1 ? 's' : '');
	} else {
		$n = floor($timeago / 86400);
		$tago = $n.' day'.($n != 1 ? 's' : '');
	}
	
	return $tago;
}

/**
 * Determines and returns whether a timestamp is from the current day.
 *
 * @var integer $time Timestamp
 *
 * @return boolean
 */
function isToday($time) {
	$today = mktime(0,0,0);
	if ($time >= $today && $time < $today+86400) {
		return true;
	} else {
		return false;
	}
}

/**
 * Get an object property value.
 *
 * @var string $property Property Name
 * @var array $object Object Info
 *
 * @return mixed Property Value
 */
function getObjectProperty($property, $object=null) {
	$tpl =& Gene::getTemplate();

	if (!$object) {
		$object = $tpl->get('object');
	}
	
	$o =& Gene_Object::get($object['object_id']);
	
	return $o->getProperty($property);
}

/**
 * Gets an user's property value.
 *
 * @var string $property Property Name
 *
 * @return mixed Property Value
 */
function getUserProperty($property) {
	$user =& Gene::getUser();
	return $user->getProperty($property);
}

/**
 * Truncates a string to a certain length.
 *
 * @var string $string Input String
 * @var integer $length Length
 *
 * @return string Truncated String
 */
function truncate($string,$length=80) {
	if (strlen($string) > $length) {
		$string = substr($string, 0, $length);
		$s = explode(" ", $string);
		$c = count($s) - 1;
		unset($s[$c]);
		$string = implode(" ", $s)."...";
	}
	
	return $string;
}

/**
 * Returns HTML Code for a pager.
 *
 * @var array $params Parameters
 *
 * @return string HTML Code for Pager
 */
function getPager($params=array()) {
	$tpl =& Gene::getTemplate();
	
	$reserved = array("object", "numpages", "currentpage", "location", "section", "showingtag", "anchor", "hidelast");
	foreach ($params as $j => $k) {
		if (in_array($j, $reserved)) {
			$tplparams[$j] = $k;
		} else {
			$args[$j] = $k;
		}
	}
	
	if ($args) {
		$tplparams['arguments'] = $args;
	}
	
	// Work out pages to display
	if ($tplparams['hidelast']) {
		$navpages = array(1,2,3,4,5,$params['currentpage']-2, $params['currentpage']-1, $params['currentpage'], $params['currentpage']+1, $params['currentpage']+2);
	} else {
		$navpages = array(1,2,3,4,5,$params['numpages']-4, $params['numpages']-3, $params['numpages']-2, $params['numpages']-1, $params['numpages'], $params['currentpage']-2, $params['currentpage']-1, $params['currentpage'], $params['currentpage']+1, $params['currentpage']+2);
	}
	
	foreach ($navpages as $j => $k) {
		if ($k < 1 OR $k > $params['numpages']) {
			unset ($navpages[$j]);
		}
	}
	
	$navpages = array_unique($navpages);
	natsort($navpages);
	
	$tplparams['showpages'] = $navpages;
	
	$tpl->assign ("pager", $tplparams);
	
	return $tpl->fetch("paging");
}

/**
 * Cycles through a list of values.
 *
 * @var array $values Array of values to cycle through
 * @var string $name Cycle name
 */
function cycle($values, $name='') {
	static $cycle = array();
	
	$id = md5($name.implode(",", $values));
	
	if (isset($cycle[$id])) {
		$cycle[$id]['current']++;
		if ($cycle[$id]['current'] == count($values)) {
			$cycle[$id]['current'] = 0;
		}
		
		return $values[$cycle[$id]['current']];
	} else {
		$cycle[$id] = array(
			"values" => $values,
			"current" => 0,
		);
		
		return $values[0];
	}
}

/**
 * Get the contents of a page.
 *
 * @var string $content Object Content
 * @var integer $page Page Number
 */
function getPage($content, $page=1) {
	$pages = explode("<!--newpage-->", $content);
	if (isset($pages[$page-1])) {
		return $pages[$page-1];
	}
}

/**
 * Returns the total number of objects in a tag cloud.
 *
 * @var array $tags Tag Cloud
 *
 * @return integer Tag Cloud Size
 */
function getTagCloudSize($tags) {
	$total = 0;
	
	foreach ($tags as $i) {
		$total += $i['objects'];
	}
	
	return $total;
}

/**
 * Returns the size of a tag.
 *
 * @var array $tag Tag
 * @var array $total Tag Cloud Size
 *
 * @return integer Tag Size
 */
function getTagSize($tag, $total) {
	$sizegap = round($total/5);
	if ($sizegap < 1) {
		$sizegap = 1;
	}
	
	return floor($tag['objects']/$sizegap);
}

/**
 * Start stripping whitespace from a portion of the template.
 */
function startStrip() {
	ob_start();
}

/**
 * End stripping whitespace from a portion of the template.
 */
function endStrip() {
	$c = ob_get_contents();
	ob_end_clean();
	
	echo preg_replace('!\s+!', ' ', $c);
}
	 
?>